package com.farcloud.data.italent.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class HttpClientUtils {

    private static final String[] IP = {"127.0.0.1", "0:0:0:0:0:0:0:1", "unknown"};

    public static String postHttp(String authorization, String url, String requestParam) {
        String str = "";
        try {
            log.info("传递北森接口入参:[{}]", requestParam);
            CloseableHttpClient client = HttpClients.createDefault();
            // 通过HttpPost来发送post请求
            HttpPost httpPost = new HttpPost(url);
        /*
         * post带参数开始
         */
            StringEntity requestEntity = new StringEntity(requestParam,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Authorization", authorization);
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
        /*
         * post带参数结束
         */
            // HttpEntity
            // 是一个中间的桥梁，在httpClient里面，是连接我们的请求与响应的一个中间桥梁，所有的请求参数都是通过HttpEntity携带过去的
            // 通过client来执行请求，获取一个响应结果
            CloseableHttpResponse response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            str = EntityUtils.toString(entity, "UTF-8");
            log.info("北森响应数据结果：[{}]", str);
            // 关闭
            response.close();
        } catch (Exception e) {
            log.info("响应北森接口数据异常:[{}]", e);
        }
        return str;
    }

    public static String HttpPostData(String authorization, String url, String jsonData) {
        log.info("响应北森获取权限数据:[{}]", jsonData);
        String result = null;
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(url);
            //添加http头信息
            StringEntity requestEntity = new StringEntity(jsonData,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httppost.setHeader("Authorization", authorization);
            httppost.setHeader("Content-type", "application/json");
            httppost.setEntity(requestEntity);
            CloseableHttpResponse response = client.execute(httppost);
            result = EntityUtils.toString(response.getEntity());
            log.info("北森响应过滤数据结果：[{}]", result);
        } catch (Exception e) {
            log.info("响应异常信息为:[{}]", e);
        }
        return result;
    }


    /**
     * 响应token结果值
     * @param appKey
     * @param appSecret
     * @return
     */
    public static String httpTokenPostData(String appKey, String appSecret) {
        String result = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://openapi.italent.cn/token");
            //添加http头信息
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            HttpResponse response;
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("grant_type", "client_credentials"));
            params.add(new BasicNameValuePair("app_key", appKey));
            params.add(new BasicNameValuePair("app_secret", appSecret));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
            httppost.setEntity(urlEncodedFormEntity);
            response = httpclient.execute(httppost);
            //检验状态码，如果成功接收数据
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
            return result;
        } catch (Exception e) {
            log.info("响应异常信息为:[{}]", e);
        }
        return result;
    }


    /**
     * 响应token结果值
     * @param appKey
     * @param appSecret
     * @return
     */
    public static String httpRefreshTokenPostData(String refreshToken) {
        String result = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://openapi.italent.cn/token");
            //添加http头信息
            httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            HttpResponse response;
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("grant_type", "refresh_token"));
            params.add(new BasicNameValuePair("refresh_token", refreshToken));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
            httppost.setEntity(urlEncodedFormEntity);
            response = httpclient.execute(httppost);
            //检验状态码，如果成功接收数据
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
            return result;
        } catch (Exception e) {
            log.info("响应异常信息为:[{}]", e);
        }
        return result;
    }



}
